<?php

namespace app\api\controller;

use app\common\basic\BaseController;
use app\common\logic\FollowUpLogic;
use support\Request;

class FollowUpController extends BaseController
{
    protected $logic;
    protected $user; 

    public function __construct()
    {
        $this->logic = new FollowUpLogic();
        $this->user = request()->user ?? [];
        parent::__construct();
    }

    /**
     * 创建跟进记录
     * @param Request $request
     * @return \support\Response
     */
    public function create(Request $request)
    {
        try {
            $data = $request->post();
            $userId = $this->user['id'] ?? 0; // 获取当前用户ID

            $result = $this->logic->createFollowUp($data, $userId);
            if (empty($result)) {
                return $this->fail('跟进记录创建失败');
            }
            return $this->success([], '跟进记录创建成功');
        } catch (\Exception $e) {
            return $this->fail($e->getMessage());
        }
    }

    /**
     * 获取跟进列表
     * @param Request $request
     * @return \support\Response
     */
    public function lists(Request $request)
    {
        try {
            $params = $request->get();
            $userId = $this->user['id'] ?? 0;

            $result = $this->logic->getFollowUpList($params, $userId);

            return $this->success($result, '获取成功');
        } catch (\Exception $e) {
            return $this->fail($e->getMessage());
        }
    }

    /**
     * 获取跟进详情
     * @param Request $request
     * @return \support\Response
     */
    public function detail(Request $request)
    {
        try {
            $id = $request->get('id');
            if (empty($id)) {
                return $this->fail('跟进ID不能为空');
            }

            $userId = $this->user['id'] ?? 0;
            $result = $this->logic->getFollowUpDetail($id, $userId);

            return $this->success($result, '获取成功');
        } catch (\Exception $e) {
            return $this->fail($e->getMessage());
        }
    }

    /**
     * 删除跟进记录
     * @param Request $request
     * @return \support\Response
     */
    public function delete(Request $request)
    {
        try {
            $id = $request->post('id');
            if (empty($id)) {
                return $this->fail('跟进ID不能为空');
            }

            $userId = $this->user['id'] ?? 0;
            $result = $this->logic->deleteFollowUp($id, $userId);

            if ($result) {
                return $this->success('删除成功');
            } else {
                return $this->fail('删除失败');
            }
        } catch (\Exception $e) {
            return $this->fail($e->getMessage());
        }
    }

    /**
     * 获取跟进方式选项
     * @param Request $request
     * @return \support\Response
     */
    public function getTypes(Request $request)
    {
        try {
            $types = $this->logic->getFollowUpTypes();
            return $this->success($types, '获取成功');
        } catch (\Exception $e) {
            return $this->fail($e->getMessage());
        }
    }

    /**
     * 获取跟进结果选项
     * @param Request $request
     * @return \support\Response
     */
    public function getResults(Request $request)
    {
        try {
            $type = $request->get('type', 1);
            $results = $this->logic->getFollowUpResults($type);
            return $this->success($results, '获取成功');
        } catch (\Exception $e) {
            return $this->fail($e->getMessage());
        }
    }

    /**
     * 获取陪同人员列表
     * @param Request $request
     * @return \support\Response
     */
    public function getPartners(Request $request)
    {
        try {
            $keyword = $request->get('keyword', '');
            $partners = $this->logic->getPartnerList($keyword);
            return $this->success($partners, '获取成功');
        } catch (\Exception $e) {
            return $this->fail($e->getMessage());
        }
    }

}